package com.user.query.concurrent;

import java.util.concurrent.*;

import static java.lang.Thread.MIN_PRIORITY;

/**
 * Created by bgt on 2017/3/14.
 */
public class Exectorsutils {
    public static void main(String[] args) throws InterruptedException {
        Exectorsutils exectorsutils=new Exectorsutils();
       // exectorsutils.test();

        Thread thread=new Thread(new simpleDaemons());
        thread.setDaemon(true);//设置守护线程
        //thread.start();
        TimeUnit.MILLISECONDS.sleep(170);
    }
    /**
     * 执行器示例
     * 线程池可以保证线程执行顺序
     */
    public  void test(){
        //单线程
        ExecutorService executorService= Executors.newSingleThreadExecutor();
        for (int i=0;i<10;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService.shutdown();

        //可以根据具体情况自己扩展线程大小
        ExecutorService executorService2= Executors.newCachedThreadPool();
        for (int i=0;i<10;i++){
            executorService2.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService2.shutdown();
        //固定数量的线程池
        ExecutorService executorService3= Executors.newFixedThreadPool(10);
        for (int i=0;i<10;i++){
            executorService3.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        executorService3.shutdown();

        //有任务返回值的调用
        ExecutorService executorServiceback= Executors.newSingleThreadExecutor();
        Future<String> tt=executorServiceback.submit(new ResultWithTask("1"));
        try {
            System.out.println(tt.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        executorServiceback.shutdown();
    }
}
/**
 * 线程类
 */
class mythread implements Runnable{
    protected  String name;
    public mythread(String name) {
        this.name=name;
    }

    @Override
    public void run() {
        System.out.println("当前线程是：："+this.name);
        try {
            //线程暂停的几种方式
            //使当前线程暂停millis所指定的毫秒，转到执行其它线程。
            Thread.sleep(100);
            //暂停当前正在执行的线程对象，并执行其他线程。
            //注意：这里的其他也包含当前线程，所以会出现以下结果。
            //Thread.yield();
            //TimeUnit.MILLISECONDS.sleep(100); //跟第一种类似
            //设置线程优先级
            //Thread.currentThread().setPriority(1);
          //  int  min=Thread.MIN_PRIORITY;//优先级
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
/**
 * 可回调任务
 */
class  ResultWithTask implements Callable<String>{
    protected  String  tt;
    public ResultWithTask(String tt){
        this.tt=tt;
    }
    @Override
    public String call() throws Exception {
        return "result:--"+this.tt;
    }
}

/**
 * 守护线程
 */
class simpleDaemons implements Runnable {
    @Override
    public void run() {

            try {
                while (true) {
                 TimeUnit.MILLISECONDS.sleep(100);
                 System.out.println("执行了"+Thread.currentThread()+this.toString());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


    }
}
